package com.lsp.base.view.animator;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.IntProperty;
import android.util.Property;
import android.widget.FrameLayout;

/**
 * @author com.lsp.ibxs
 * @created 16/12/1 16:33
 */
@TargetApi(Build.VERSION_CODES.N)
public class ForegroundFrame extends FrameLayout {
  public ForegroundFrame(Context context) {
    super(context);
  }

  public ForegroundFrame(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public ForegroundFrame(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  public final Property<ForegroundFrame, Integer> FOREGROUND_COLOR =
      new IntProperty<ForegroundFrame>("foregroundColor") {
        @SuppressLint("NewApi") @Override public void setValue(ForegroundFrame object, int value) {
          if (object.getForeground() instanceof ColorDrawable) {
            ((ColorDrawable) object.getForeground().mutate()).setColor(value);
          } else {
            object.setForeground(new ColorDrawable(value));
          }
        }

        @SuppressLint("NewApi") @Override public Integer get(ForegroundFrame object) {
          if (object.getForeground() instanceof ColorDrawable) {
            return ((ColorDrawable) object.getForeground()).getColor();
          } else {
            return Color.TRANSPARENT;
          }
        }
      };
}
